<?php
class wapi_events extends wapi_mod_base {
	function _construct(){
		if ($this->firstrun){
			$this->db_create();
		}
	}
	function db_create(){
		global $wapi;
		$sql = "CREATE TABLE event_log (
		id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
		event TEXT NOT NULL,
		user_id TEXT NOT NULL,
		session_id TEXT NOT NULL,
		raisedby_class TEXT NOT NULL,
		raisedby_function TEXT NOT NULL,
		time TEXT NOT NULL
		)";
		$wapi->db->exec($sql);
		$sql = "CREATE TABLE event_register (
		id TEXT PRIMARY KEY UNIQUE NOT NULL,
		event TEXT NOT NULL,
		app_class TEXT NOT NULL,
		condition TEXT,
		enabled INTEGER NOT NULL
		)";
		$wapi->db->exec($sql);
	}
	function log($event){
		global $wapi;

	}
	function raise($event){
		// raise an app event
		// any app that is registered for this event
		// and events conditions are met will be loaded
		// and the appropriate function will be called
		// when calling registered event handler report the class that called it....
		// might not be necessary as the called handler could just use backtrace
		global $wapi;
		$cnt = 0;
		$backtrace = debug_backtrace();
		$raisedby_class = $backtrace[1]['class'];
		$raisedby_function = $backtrace[1]['function'];
		$time = date("Y-m-d H:i:s",time());
		$sql = "INSERT INTO event_log (event,user_id,session_id,raisedby_class,raisedby_function,time) VALUES ('$event','{$wapi->user->user_id}','$wapi->session_id','$raisedby_class','$raisedby_function','$time')";
		$wapi->db->exec($sql);
		// raise the event in any mods that are setup for it
		$func = 'event_' . $event;
		if (method_exists($wapi, $func)){
			$wapi->$func();
		}
		foreach ($wapi->mods as $m){
			if (method_exists($m, $func)){
				$m->$func();
			}
		}
		// raise the event in any apps that are setup for it
		$sql = "SELECT * FROM event_register WHERE enabled = 1 AND event = '$event';";
		if ($wapi->db->query($sql)){
			$res = $wapi->db->results;
			foreach ($res as $r){
				$runit = true;
				if ((bool) $r['condition']){
					$runit = (bool) eval($r['condition']);
				}
				if ($runit){
					$cnt ++;
					$stop = '';
					// event handler conditions are met
					// if app exists AND is enabled
					// load the app and run the event function
//					if (method_exists($m, $func)){
//						$m->$func();
//					}

				}
			}
		}
		return $cnt;
	}
	function register($event,$condition='',$app_class=null){
		global $wapi;
		if ((bool) $app_class) {
			$backtrace = debug_backtrace();
			$app_class = $backtrace[1]['class'];
		}
		// if app exists AND is enabled
		$id = $app_class . ' ' . $event;
		$condition = $wapi->db->esc($condition);
		$sql = "INSERT INTO event_register (id,event,app_class,condition,enabled) VALUES ('$id','$event','$app_class','$condition',1)";
		if ($wapi->db->exec($sql) == 1) {
			return true;
		}
		return false;
	}
	function is_registered($event,$app_class=null){
		global $wapi;
		if ((bool) $app_class) {
			$backtrace = debug_backtrace();
			$app_class = $backtrace[1]['class'];
		}
		$sql = "SELECT id FROM event_register WHERE app_class = '$app_class' AND event = '$event'";
		if ($wapi->db->query($sql)){
			return true;
		}
		return false;
	}
	function get_condition($event,$app_class=null){
		global $wapi;
		$condition = '';
		if ((bool) $app_class) {
			$backtrace = debug_backtrace();
			$app_class = $backtrace[1]['class'];
		}
		$sql = "SELECT condition FROM event_register WHERE app_class = '$app_class' AND event = '$event'";
		if ($wapi->db->query($sql)){
			$res = $wapi->db->results;
			$r = $res[0];
			$condition = $r['condition'];
		}
		return $condition;
	}
	function unregister($event,$app_class=null){
		global $wapi;
		if ((bool) $app_class) {
			$backtrace = debug_backtrace();
			$app_class = $backtrace[1]['class'];
		}
		// if app exists AND is enabled
		$id = $app_class . ' ' . $event;
		$condition = $wapi->db->esc($condition);
		$sql = "DELETE FROM event_register WHERE app_class = '$app_class' AND event = '$event'";
		if ($wapi->db->exec($sql) == 1) {
			return true;
		}
		return false;
	}
	function disable($event,$app_class=null){
		global $wapi;
		if ((bool) $app_class) {
			$backtrace = debug_backtrace();
			$app_class = $backtrace[1]['class'];
		}
		// if app exists AND is enabled
		$sql = "UPDATE event_register SET enabled = 0 WHERE app_class = '$app_class' AND event = '$event'";
		if ($wapi->db->exec($sql) == 1) {
			return true;
		}
		return false;
	}
	function enable($event,$app_class=null){
		global $wapi;
		if ((bool) $app_class) {
			$backtrace = debug_backtrace();
			$app_class = $backtrace[1]['class'];
		}
		// if app exists AND is enabled
		$sql = "UPDATE event_register SET enabled = 1 app_class = '$app_class' AND event = '$event'";
		if ($wapi->db->exec($sql) == 1) {
			return true;
		}
		return false;
	}
}
?>